#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtGui>
#include <QRadioButton>
#include "bass.h"
#include <deque>

#define E_major_number 0 /* 82.41  */
#define A_major_number 5 /* 110.00 */
#define D_minor_number 10/* 147.83 */
#define G_minor_number 15/* 196.00 */
#define B_minor_number 19/* 246.96 */
#define E_first_number 24/* 329.63 */

class QPushButton;
class QEvent;
class QKeyEvent;
class QSlider;
class QLabel;

extern double koef;
extern double k;
extern DWORD SoundSpeed;
extern const int KEYS;
extern const WORD keys[];

extern int place;
extern int strings[150][88200];
extern int a[51];
extern std::deque<int> samples;
extern int position[6];
extern HSTREAM streams[6];
extern bool chorusIsTrue;
extern bool harmonizedIsTrue;

void run_all();

class OurWindow: public QDialog{
    Q_OBJECT

private:
    QPushButton *closeButton;
    QRadioButton *radioButton1;
    QRadioButton *radioButton2;
    QRadioButton *radioButton3;
    QRadioButton *radioButton4;
    QSlider *Slider;
    QSlider *Frets;
    QLabel *label;
    QLabel *fretsLabel;

private slots:
    void first()
    {
        k = 2;
        koef = 0.999;
        chorusIsTrue = false;
        harmonizedIsTrue = false;
        run_all();
    }

   void second()
    {
        k = 0.5;
        koef = 0.85;
        chorusIsTrue = false;
        harmonizedIsTrue = false;
        run_all();
    }

   void third(){
       k = 2;
       koef = 0.999;
       chorusIsTrue = true;
       harmonizedIsTrue = false;
       run_all();
   }


   void four(){
       k = 2;
       koef = 0.999;
       harmonizedIsTrue = true;
       chorusIsTrue = false;
       run_all();
   }

   /*int ValueIs(){
        if (Slider->value() == 1)
            return 11024;
        else if (Slider->value() == 2)
            return 22050;
        else if (Slider->value() == 3)
            return 44100;
        else if (Slider->value() == 4)
            return 88200;
   } */
   public slots:
   void SpeedChanged(int a){
       if (a == 1)
           SoundSpeed = 11024;
       else if (a == 2)
           SoundSpeed = 22050;
       else if (a == 3)
           SoundSpeed = 44100;
       else if (a == 4)
           SoundSpeed = 88200;
   }
   void FretsChanged(int a){
       place = a;
   }

public:
    OurWindow(QWidget * parent = 0){
        closeButton = new QPushButton(tr("Close"));
        radioButton1 = new QRadioButton(tr("Normal"));
        radioButton2 = new QRadioButton(tr("Palm"));
        radioButton3 = new QRadioButton(tr("Chorus"));
        radioButton4 = new QRadioButton(tr("Harmonized"));
        Slider = new QSlider(Qt::Horizontal);
        Slider->setRange(1, 4);
        Frets = new QSlider(Qt::Horizontal);
        Frets->setRange(0, 4);
        fretsLabel = new QLabel(tr("Frets position"));
        label = new QLabel(tr("Sound Speed"));
        connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
        connect(radioButton1, SIGNAL(clicked()), this, SLOT(first()));
        connect(radioButton2, SIGNAL(clicked()), this, SLOT(second()));
        connect(radioButton3, SIGNAL(clicked()), this, SLOT(third()));
        connect(radioButton4, SIGNAL(clicked()), this, SLOT(four()));
        connect(Slider, SIGNAL(valueChanged(int)), this, SLOT(SpeedChanged(int)));
        connect(Frets, SIGNAL(valueChanged(int)), this, SLOT(FretsChanged(int)));
        QVBoxLayout *leftLayout = new QVBoxLayout;
        leftLayout->addWidget(radioButton1);
        leftLayout->addWidget(radioButton2);
        leftLayout->addWidget(radioButton3);
        leftLayout->addWidget(radioButton4);
        radioButton1->setChecked(true);
        QVBoxLayout *rightLayout = new QVBoxLayout;
        rightLayout->addWidget(label);
        rightLayout->addWidget(Slider);
        rightLayout->addWidget(fretsLabel);
        rightLayout->addWidget(Frets);
        Slider->setValue(2);
        Frets->setValue(1);
        rightLayout->addWidget(closeButton);
        rightLayout->addStretch();
        QHBoxLayout *mainLayout = new QHBoxLayout;
        mainLayout->addLayout(leftLayout);
        mainLayout->addLayout(rightLayout);
        setLayout(mainLayout);
        setWindowTitle("Guitar");
        setFixedHeight(sizeHint().height());
    }
protected:
    void keyPressEvent(QKeyEvent * event){

        if (event->key() == Qt::Key_Equal)
            if (SoundSpeed == 11024)
                SoundSpeed = 22050;
            else if (SoundSpeed == 22050)
                SoundSpeed = 44100;
            else if (SoundSpeed == 44100)
                SoundSpeed = 88200;
        if (event->key() == Qt::Key_Minus)
            if (SoundSpeed == 88200)
                SoundSpeed = 44100;
            else if (SoundSpeed == 44100)
                SoundSpeed = 22050;
            else if (SoundSpeed = 22050)
                SoundSpeed = 11024;
        if (event->key() == Qt::Key_M)
            place == 4 ? place : ++place;
        if (event->key() == Qt::Key_N)
            place == 0 ? place : --place;
        for (int key = 0; key < KEYS; ++key)
            if (event->key() == keys[key])
                BASS_StreamPutData(streams[key / 5], strings[position[key / 5] + key % 5 + place * 5], SoundSpeed);
    }
};

#endif // DIALOG_H
